// service/merchant/merchant.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    list:[],
    finish: false,
    loading: false,
    page: 1,
    code: '',
    state: '',
    shop: '',
    shop_id: '',
    info: '',
  },
  setCode(e){
    this.setData({
      code: e.detail.value
    })
  },
  toDetail(e) {
    util.skip('/service/recruitDetail/recruitDetail',{}, (res)=>{
      res.eventChannel.emit('shopInfo', e.currentTarget.dataset.info)
    })
  },
  joined() {
    if (this.data.code.trim() === '') return util.toast('请输入商家工号')
    http.post('shop/getCodeShop',{code: this.data.code}).then(res => {
      this.setData({
        show: true,
        info: res.data
      })
    })
    
  },
  certainJoin(){
    http.post('skill/joinshop',{code: this.data.code,type:1}).then(res => {
      util.toast(res.msg)
      this.setData({
        show: false
      })
    })
  },
  reload() {
    this.setData({
      list: [],
      page: 1,
      finish: false
    })
    this.getList()
  },
  more() {
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  getList() {
    if (this.data.finish) {
      return
    }
    if (this.data.loading) {
      return
    }
    this.setData({
      loading: true
    })
    http.get('shop/shoplist', {
      page: this.data.page,
    }).then(res => {
      if (res.data.length === 0) {
        this.setData({
          finish: true
        })
      }
      let arr = this.data.list.concat(res.data)
      this.setData({
        list: arr
      })

    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },
  getShop(){
    http.get('shop/shopdetail', {
      id: this.data.shop_id,
    }).then(res => {
      this.setData({
        shop: res.data
      })
      wx.setNavigationBarTitle({
        title: res.data.name || '店铺信息',
      })
    })
  },
  call(){
    wx.makePhoneCall({
      phoneNumber: this.data.shop.leader_mobile
    })
  },
  openLoaction(){
    wx.openLocation({
      latitude : Number(this.data.shop.lat),
      longitude : Number(this.data.shop.lng),
      name: this.data.info.address,
      scale: 18
    })
  },
  getInfo(){
    http.post('skill/joinshop',{},true).then(res => {
      if(res.data.skill.shop_id){
        this.setData({
          shop_id: res.data.skill.shop_id
        })
        this.getShop()
      }else if(res.data.joinShop){
        this.setData({
          code: res.data.joinShop.code,
          state: res.data.joinShop.state,
          shop: res.data.skill
        })
      }else {
        this.setData({
          shop: res.data.skill
        })
      }
      
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.reload()
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})